import 'package:cached_network_image/cached_network_image.dart';
import 'package:elephantgas/common/constant.dart';
import 'package:elephantgas/network/network_manager.dart';
import 'package:elephantgas/network/network_url.dart';
import 'package:elephantgas/routers/routers.dart';
import 'package:elephantgas/utils/toast_util.dart';
import 'package:elephantgas/widget/cities/src/cities_selector/alpha.dart';
import 'package:elephantgas/widget/myappbar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_card_swipper/flutter_card_swiper.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:elephantgas/bean/mall_goods_detail_entity.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:elephantgas/widget/big_image.dart';
class MallGoodsDetailPage extends StatefulWidget {
  String id;
  MallGoodsDetailPage(this.id);
  @override
  _MsgState createState() => _MsgState();
}

class _MsgState extends State<MallGoodsDetailPage> {
  @override
  void initState() {
    super.initState();
    _getData();
  }

  MallGoodsDetailData mData = null;
  List<String> _imgList = [];
  _getData() {
    Map map = {'id': widget.id};
    DioManager.getInstance().postJson(ServiceUrl.shop_goods_detail, map,
        (data) {
      Map queryBack = new Map<String, dynamic>.from(data);
      MallGoodsDetailEntity homeEntity =
          new MallGoodsDetailEntity().fromJson(queryBack);
      mData = homeEntity.data;
      if (mData.majorImageUrl != null) {
        _imgList = mData.majorImageUrl.split(',');
      }
      for(int i=0;i<_imgList.length;i++){
        if(!_imgList[i].startsWith("http")){
          _imgList[i] = Constant.img_baseUrl +_imgList[i];
        }
      }
      print(_imgList.length);
      setState(() {});
    }, (error) {
      ToastUtil.show(error);
    });
  }


  Widget _homeSwiper() {
    return ConstrainedBox(
        child: Swiper(
          itemCount: _imgList.length,
          outer: false,
          autoplay: true,
          // ignore: missing_return
          onIndexChanged: (index){
            nowIndex = (index+1).toString();
          },
          onTap: (index){
            Navigator.push(
              context,
              new MaterialPageRoute(
                builder: (context) => new PhotoViewGalleryScreen(
                  images:_imgList,//传入图片list
                  index: index,//传入当前点击的图片的index
                  heroTag: '11',//传入当前点击的图片的hero tag （可选）
                ),
              ),
            );

          },
          itemBuilder: (context, index) {
            // if (swiperDataList != null) {
            return CachedNetworkImage(
              // imageUrl: "${swiperDataList[index].imgUrl}",
              imageUrl: _imgList[index],
              // placeholder: (context, url) =>
              //     new CircularProgressIndicator(),
              errorWidget: (context, url, error) => new Icon(Icons.error),
              fit: BoxFit.fitWidth,
            );
            // }
          },
          pagination: SwiperCustomPagination(
              builder: (BuildContext context, SwiperPluginConfig config) {
                return Stack(
                  children: [
                    Positioned(
                        bottom: 0,
                        right: 0,
                        child: Container(
                            alignment: Alignment.center,
                            height: 18,
                            margin: EdgeInsets.only(right: 15, bottom: 10),
                            padding: EdgeInsets.only(left: 10, right: 10),
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(9),
                                color: Color(0xFF8A8D90)),
                            child: Text(
                              nowIndex + '/' + _imgList.length.toString(),
                              style: TextStyle(
                                  color: Colors.white, fontSize: 12.sp),
                            )))
                  ],
                );
              }),

        ),
        constraints: new BoxConstraints.loose(
            new Size(MediaQuery.of(context).size.width, 210)),
    );
  }

String nowIndex = '1';

  Widget _buildPrice() {
    return Row(
      children: [
        Text(
          '${mData.exchangePrice}',
          style: TextStyle(color: Color(0xFFEB5525), fontSize: 24),
        ),
        Text(
          '积分',
          style: TextStyle(color: Color(0xFFEB5525), fontSize: 12.sp),
        ),
        SizedBox(
          width: 9,
        ),
        Text(
          '${mData.linePrice}元',
          style: TextStyle(
              decoration: TextDecoration.lineThrough,
              color: Color(0xFF868B9A),
              fontSize: 12.sp),
        ),
      ],
    );
  }

  Widget _buildName() {
    return Row(
      children: [
        Text(
          '${mData.goodsName}',
          style: TextStyle(
              color: Color(0xFF3C4353),
              fontSize: 14.sp,
              fontWeight: FontWeight.bold),
        ),
      ],
    );
  }

  Widget _buildOther() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          height: 12.5,
        ),
        Text(
          '规格/型号：${mData.goodsNorm}',
          style: TextStyle(
            color: Color(0xFF1C1D21),
            fontSize: 12.sp,
          ),
        ),
        SizedBox(
          height: 8.5,
        ),
        Text(
          '库存：${mData.inventory}',
          style: TextStyle(
            color: Color(0xFF1C1D21),
            fontSize: 12.sp,
          ),
        ),
        SizedBox(
          height: 8.5,
        ),
        Text(
          '品牌：${mData.goodsBrand}',
          style: TextStyle(
            color: Color(0xFF1C1D21),
            fontSize: 12.sp,
          ),
        ),
        SizedBox(
          height: 14,
        ),
      ],
    );
  }

  Widget _buildTip() {
    return Container(
      margin: EdgeInsets.only(left: 16),
        alignment: Alignment.center,
        height: 35,
        child: Row(
          children: [
            Image.asset(
              Constant.ASSETS_IMG_COMMON + 'house_goods_tip.png',
              width: 16,
              height: 16,
            ),
            SizedBox(
              width: 7,
            ),
            Text(
              '不支持退货',
              style: TextStyle(color: Color(0xFF3C4353), fontSize: 12.sp),
            )
          ],
        ));
  }

  Widget _buildDesc() {
    return Column(
      children: [
        SizedBox(
          height: 16,
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: 13,
              height: 0.5,
              color: Color(0xFF353535),
            ),
            SizedBox(
              width: 12.5,
            ),
            Text(
              '详情',
              style: TextStyle(color: Color(0xFF333333), fontSize: 14.sp),
            ),
            SizedBox(
              width: 12.5,
            ),
            Container(
              width: 13,
              height: 0.5,
              color: Color(0xFF353535),
            ),
          ],
        ),
        Html(data: mData.description)
      ],
    );
  }

  Widget _buildContent() {
    return Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            height: 16,
          ),
          Container(
            margin: EdgeInsets.only(left: 16),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                _buildPrice(),
                _buildName(),
                _buildOther(),
              ],
            ),
          ),

          Container(
            color: Color(0xFFF3F4F5),
            height: 6,
          ),
          _buildTip(),
          Container(
            color: Color(0xFFF3F4F5),
            height: 6,
          ),
        ],
      ),
    );
  }

  Widget _buildBottom() {
    return InkWell(
        onTap: () {
          Routers.navigateTo(context, Routers.mall_order_confirm,
              params: {'id': mData.id}).then((value) {
            if (value == '11') {
              Navigator.of(context).pop('11');
            }
          });
        },
        child: Container(
          width: MediaQuery.of(context).size.width - 32,
          margin: EdgeInsets.only(left: 16, right: 16),
          alignment: Alignment.center,
          height: 48,
          decoration: BoxDecoration(
              color: Color(0xFF2979FF), borderRadius: BorderRadius.circular(4)),
          child: Text(
            '立即兑换',
            style: TextStyle(color: Colors.white, fontSize: 16.sp),
          ),
        ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        context: context,
        title: '商品详情',
      ),
      body: Container(
          height: MediaQuery.of(context).size.height,
          child: Stack(
            children: [
              SingleChildScrollView(
                child: Container(
                  child: Column(
                    children: [_homeSwiper(), _buildContent(), _buildDesc()],
                  ),
                ),
              ),
              Positioned(bottom: 14, child: _buildBottom())
            ],
          )),
    );
  }
}
